package moni.anyou.com.view.view;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import moni.anyou.com.view.R;
import moni.anyou.com.view.view.account.LoginActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.view.*;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
*  功能描述：引导界面activity类
*/
public class GuideActivity extends Activity{
	private ViewPager viewPager; // android-support-v4中的滑动组件
	private MyAdapter myAdapter;
	int[] imageResId = new int[] { R.drawable.start_1, R.drawable.start_2,
			R.drawable.start_3 ,R.drawable.start_4};
	public int currentItem;
	private int currentPageScrollStatus;
	private int maxPos = 3;
	private int pos = 0;
	private List<ImageView> imageViews; // 滑动的图片集合
   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				            WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_guide);
	    initViewPager();
	}

	@SuppressLint("NewApi")
	private void initViewPager() {
		imageViews = new ArrayList<ImageView>();
		try {
			for (int i = 0; i < imageResId.length; i++) {
				ImageView imageView = new ImageView(this);
				imageView.setImageResource(imageResId[i]);
				imageView.setScaleType(ScaleType.CENTER);
				imageViews.add(imageView);
			}
		} catch (Exception ex) {
			// System.out.println(ex.toString());
		}

		viewPager = (ViewPager) findViewById(R.id.head_viewpager);

		myAdapter = new MyAdapter();
		viewPager.setAdapter(myAdapter);// 设置填充ViewPager页面的适配器
		// 设置一个监听器，当ViewPager中的页面改变时调用
		viewPager.setOnPageChangeListener(new MyPageChangeListener());

	}

	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
		private int oldPosition = 0;

		/**
		 * This method will be invoked when a new page becomes selected.
		 * position: Position index of the new selected page.
		 */

		public void onPageScrollStateChanged(int arg0) {
			currentPageScrollStatus = arg0;
		}

		public void onPageScrolled(int arg0, float positionOffset,
				int positionOffsetPixels) {

			if (pos == maxPos) {
				// 已经在最后一页还想往右划
				if (positionOffsetPixels == 0 && currentPageScrollStatus == 1) {
                    Intent intent = new Intent(GuideActivity.this,IndexActivity.class);
					startActivity(intent);
					GuideActivity.this.finish();
				}
			}

		}

		@Override
		public void onPageSelected(int position) {
			// 切换page设置当前position值
			setCurrentPos(position);
		}

		public void setMaxPage(int position) {
			// 设置最后一页的position值
			maxPos = position;
		}

		public void setCurrentPos(int position) {
			// 设置当前页的position值
			pos = position;
		}
	}

	/**
	 * 填充ViewPager页面的适配器
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// Field imageViews;

			((ViewPager) arg0).addView((View) imageViews.get(arg1));
			((View) imageViews.get(arg1)).setTag(arg1);
			((View) imageViews.get(arg1))
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
                            
							if (v.getTag() != null && ((Integer)v.getTag()).intValue()  == maxPos) {
                            
			                    Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
								intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);   
								startActivity(intent);
								GuideActivity.this.finish();
							} else {
							
							     	viewPager.setCurrentItem(((Integer)v.getTag()).intValue()+1);
								
							}
						}
					});

			return imageViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {

		}

	}





	

}